package com.framework.modules.notice;

import com.framework.exception.util.RespResult;
import com.framework.modules.oss.cloud.OSSFactory;
import com.framework.modules.sys.controller.AbstractController;
import com.framework.page.PageUtils;
import com.framework.service.yhbservice.YhbNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@RequestMapping("sys/yhbNotice")
@RestController
public class YhbNoticeController  extends AbstractController {
    @Autowired
    private YhbNoticeService yhbNoticeService;

    @RequestMapping("list")
    public RespResult list(@RequestParam Map<String,Object> params){
        params.put("deptId",getDeptId());
        PageUtils page=yhbNoticeService.queryNoticePage(params);
        return RespResult.ok().put("page",page);
    }


    /**
     * 增加通知
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/add")
    public RespResult addNotice(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        Long deptId=getUser().getDeptId();

        String prefix="";
        if(deptId.equals(2L)){
            prefix="yhb/notice";
        } else{
            prefix="yhb/notice";
        }
        String coverSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String coverUrl = OSSFactory.build().uploadSuffix(file.getBytes(), coverSuffix,prefix);
        yhbNoticeService.insertNewNotice(coverUrl,deptId,request);
        return RespResult.ok();
    }

    @RequestMapping("/delete/{noticeNo}")
    public RespResult delete(@PathVariable String noticeNo){
        yhbNoticeService.deleteById(noticeNo);
        return  RespResult.ok();
    }


}
